package com.atguigu.service.impl;


import com.atguigu.mapper.UserMapper;
import com.atguigu.pojo.User;
import com.atguigu.service.UserService;
import com.atguigu.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private RedisTemplate redisTemplate;

    @Override
    @Transactional
    public Result getUserList() {
        //使用redisTemplate获取key
        List<User> userList=
                (List<User>) redisTemplate.boundValueOps("userList").get();
        if(userList==null){
            List<User> users = userMapper.selectAll();
            redisTemplate.boundValueOps("userList").set(users);
           return  Result.ok(users);
        }
        return  Result.ok(userList);
    }
}
